package com.heart.house.biz.service;

import com.heart.house.biz.mapper.AgencyMapper;
import com.heart.house.common.model.User;
import com.heart.house.common.page.PageData;
import com.heart.house.common.page.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Luo Zhen
 * @create 2019-06-25 9:49
 */
@Service
public class AgencyService {

    @Autowired
    private AgencyMapper agencyMapper;
    @Value("${file.prefix}")
    private String imgPrefix;

    /**
     * 访问user表获取详情
     * 添加用户头像前缀
     *
     * @param userId
     * @return
     */
    public User getAgentDetail(Long userId) {
        User user = new User();
        user.setId(userId);
        user.setType(2);
        List<User> list = agencyMapper.selectAgent(user, PageParams.build(1, 1));
        this.setImg(list);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    private void setImg(List<User> list) {
        list.forEach(u -> {
            u.setAvatar(imgPrefix + u.getAvatar());
        });
    }

    public PageData<User> getAllAgent(PageParams pageParams) {
        List<User> agentList = agencyMapper.selectAgent(new User(), pageParams);
        this.setImg(agentList);
        Long count = agencyMapper.selectAgentCount(new User());
        return PageData.buildPage(agentList, count, pageParams.getPageSize(), pageParams.getPageNum());
    }
}
